package com.atguigu.mySmartCampus.controller;

import com.atguigu.mySmartCampus.Service.StudentService;
import com.atguigu.mySmartCampus.entity.Student;
import com.atguigu.mySmartCampus.util.MD5;
import com.atguigu.mySmartCampus.util.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//..@Api("value=,tags=学生")
@RestController
@Api("tags=学生")
@RequestMapping("/sms/studentController")
public class StudentController {

    @Autowired
    private StudentService studentService;
//    public Result login(LoginForm loginForm){
//        if(loginForm!=null){
    //          Student student =studentService.login(loginForm);
    //        return Result.ok(student);
    //  }
    //return Result.fail();
    //}

    // public Result getStudentById(){

    //  }

    @DeleteMapping("/delStudentById")
    //..public Result deleteStudent(@RequestBody Student student){
    public Result delStudentById(@ApiParam("删除n个学生id") @RequestBody List<Integer> ids) {
        studentService.removeByIds(ids);
        return Result.ok();
    }

    @PostMapping("/addOrUpdateStudent")
    public Result addOrUpdateStudent(@ApiParam("新增保存学生") @RequestBody Student student) {
        Integer id = student.getId();
        if (id == null || id == 0) {
//student.setPassword(MD5.encypt(student.getPassword())) 
            student.setPassword(MD5.encrypt(student.getPassword()));
        }
        studentService.saveOrUpdate(student);
        return Result.ok();
    }

    //.. @GetMapping("getStudetnByOperator")
    //          /getStudentByOpr/1/3
//..    @GetMapping("getStudetnByOpr/{pageNo}/{pageSize}")
    //..   public Result getStudetnByOperator(Page<Student> page,Student student){
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result getStudentByOpr(@ApiParam("页码数") @PathVariable Integer pageNo,
                                       @ApiParam("页大小") @PathVariable Integer pageSize,
                                       @ApiParam("学生")  Student student) {
        //..Page page = new Page();
        System.out.println("StudentController.getStudentByOpr"
        +"\n-->pageNo= "+pageNo
        +"\n-->pageSize= "+pageSize
        +"\n-->student= "+student
        );
        Page<Student> page = new Page(pageNo, pageSize);
        IPage<Student> pageList = studentService.getStudentsByOperator(page, student);
        return Result.ok(pageList);
    }


}